// 商品详情页
const app = getApp()
const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsID: "",
    title:"",
    des:"",
    price:"",
    photos:[],
    qq:"",
    wx:"",
    openid: [],
    aixin:"../../icons/aixin.png",
    hasAddToBuyCar: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
    //获取当前商品id
    this.getThisGoods(options)
    //根据购物车情况显示收藏图标
    this.isInDB()
  },

  //获取当前商品数据
  getThisGoods(options){
    var that = this;
    that.setData({
      goodsID: options.goodsID
    })
    db.collection("goods").doc(that.data.goodsID).get().then(res=>{
      that.setData({
        title : res.data.name,
        des: res.data.content,
        price: res.data.price,
        photos: res.data.photos,
        qq: res.data.qq,
        wx: res.data.wx,
      })
    })
  },
  
  //是否已加入购物车
  isInDB(){
    var flag = false
    app.appGetOpenId().
    then(res=>{
      db.collection('mine').where({
        _openid: _.eq(res),
        buyCar: _.all([this.data.goodsID])
      }).get()
      .then(res=>{
        if(res.data.length!=0){
          this.setData({
            aixin:"../../icons/aixin2.png",
            hasAddToBuyCar: true
          })
          flag = true
        }
        console.log('is in db',flag)
        return flag
      })
    })
  },

  collect(){
    if(this.data.hasAddToBuyCar==false){
      this.setData({
        aixin:"../../icons/aixin2.png",
        hasAddToBuyCar: true
      })
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        duration: 1500
      })
      //writeToLocal()
      this.addtoDB()
    }else{
      this.setData({
        aixin:"../../icons/aixin.png",
        hasAddToBuyCar: false
      })
      wx.showToast({
        title: '取消收藏成功',
        icon: 'success',
        duration: 1500
      })
      //writeToLocal()
      this.deletetoDB()
    }
  
  },

  //加入购物车后的数据操作
  addtoDB(){
    app.appGetOpenId().then(res=>{
      db.collection('mine').where({
        _openid: res
      }).update({
        data:{
          buyCar: _.push(this.data.goodsID)
        }
      })
    })
  },

  //取消加入购物车后的数据操作
  deletetoDB(){
    app.appGetOpenId().then(res=>{
      db.collection('mine').where({
        _openid: res
      }).update({
        data:{
          buyCar: _.pull(this.data.goodsID)
        }
      })
      console.log('delete data did')
    })
  },

  //复制qq到剪贴板
  copyQQ: function (e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制qq成功'
            })
          }
        })
      }
    })
  },

  //复制wx到剪贴板
  copyWX: function (e) {
    if(this.data.wx.length > 1){
      wx.setClipboardData({
        data: e.currentTarget.dataset.text,
        success: function (res) {
          wx.getClipboardData({
            success: function (res) {
              wx.showToast({
                title: '复制wx成功'
              })
            }
          })
        }
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '卖家未上传微信',
      })
    }
  },
})